package main

import (
	"fmt"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	done := make(chan struct{})
	ch := make(chan interface{}, 10)
	workerCount := 10
	for i := 0; i < workerCount; i++ {
		wg.Add(1)
		go doIt(i, ch, done, &wg)
	}

	for i := 0; i < workerCount; i++ {
		ch <- i
	}

	close(ch)
	//time.Sleep(2 * time.Second)
	close(done)
	wg.Wait()
	fmt.Println("所有的都完成了")
}

func doIt(workerId int, ch <-chan interface{}, done <-chan struct{}, wg *sync.WaitGroup) {
	fmt.Printf("[%v] 在跑\n", workerId)
	defer wg.Done()
	for {
		select {
		case m, ok := <-ch:
			if ok {
				fmt.Printf("[%v]   m => %v\n", workerId, m)
			}
		case <-done:
			fmt.Printf("[%v]            done\n", workerId)
			return

		}
	}
}
